﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckUserConnectionsNotDefault:IssueTypeCheckBase
    {

        public ServiceCheckUserConnectionsNotDefault()
        {
            IssueTypeId = 2008;
        }

        public override void Check()
        {
        
            HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


            /*
             * This rule checks if the User Connections are have been set and not allowing the max number of concurrent connections..
             * It is recommended that you set the Max Concurrent User Connections setting to the default of “0” to allow SQL Server to use free memory to store additional data/index pages or additional query plans.
             */

            var Qry = from ss in dc.ServiceSettings

                      where (ss.Name == "user connections") && (Convert.ToInt32(ss.Value) != 0)


                      select ss;  

            foreach (ServiceSetting serviceSetting in Qry)
            {
                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                //Create new Issue

                Issue issue = new Issue();
                issue.IssueType = issueType;
                issue.TargetId = serviceSetting.ServiceId;

               

                issue.Description = issueType.Description.Replace("{InstanceName}", serviceSetting.Service.InstanceName);

                dc.Issues.InsertOnSubmit(issue);
            }

            dc.SubmitChanges();
        
        }
    
    }
}
